<style>
.pure_table_extra { width: 230px; border: 1px solid #aaa !important; }
.pure_table_extra a { color: #666; }
.pure_table_extra * { font-size: 12px !important; font-family: Trebuchet MS; }
.pure_table_extra .date { float: right; color: #666; font-size: 11px !important; }
</style>

<?php
$cat = Utils::get_param('cat', NULL);

$opts = new stdClass();
$opts->cd = 't.tag_id LIKE :v1';
$opts->cdv = array(':v1' => '%,' . $cat . ',%');
$SIZE = 10;
$pops = $orm->get('pb_ecom_article', 't.visited__', 'DESC', 1, $SIZE, $opts)->d;
$lats = $orm->get('pb_ecom_article', 't.date', 'DESC', 1, $SIZE, $opts)->d;
?>

<table class="pure-table pure_table_extra" cellPadding="0" cellSpacing="0">
  <tr><td class="ui-widget-header">浏览最多</td></tr>
  <?php foreach ($pops as $idx => &$itm) { ?>
  <tr class="<?php echo ($idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd'; ?>">
    <td>
      <span class="date"><?php echo date('Y-m-d', strtotime($itm->date)); ?></span>
      <a href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=' + __id + '&__file=index_article&id=<?php echo $itm->id?>&cat=<?php echo $cat?>');"><?php echo Utils::get_short($itm->title, 20);?></a>
    </td>
  </tr>
  <?php } ?>
  <?php for ($i = 10 - count($pops), $il = 0; $i > $il; $i--) { ?>
  <tr class="<?php echo ($i % 2 == 0) ? 'pure-table-even' : 'pure-table-odd'; ?>"><td>&nbsp;</td></tr>
  <?php } ?>
</table>
<div>&nbsp;</div>
<table class="pure-table pure_table_extra" cellPadding="0" cellSpacing="0">
  <tr><td class="ui-widget-header">最新文章</td></tr>
  <?php foreach ($lats as $idx => &$itm) { ?>
  <tr class="<?php echo ($idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd'; ?>">
    <td>
      <span class="date"><?php echo date('Y-m-d', strtotime($itm->date)); ?></span>
      <a href="#" onclick="return Url.redirect(__redmd, __sys + '.php?i=' + __id + '&__file=index_article&id=<?php echo $itm->id?>&cat=<?php echo $cat?>');"><?php echo Utils::get_short($itm->title, 20);?></a>
    </td>
  </tr>
  <?php } ?>
  <?php for ($i = 10 - count($lats), $il = 0; $i > $il; $i--) { ?>
  <tr class="<?php echo ($i % 2 == 0) ? 'pure-table-even' : 'pure-table-odd'; ?>"><td>&nbsp;</td></tr>
  <?php } ?>
</table>
<div>&nbsp;</div>
<table class="pure-table pure_table_extra" cellPadding="0" cellSpacing="0">
  <thead><tr><th class="ui-widget-header">浏览过的文章</th></tr></thead>
  <tbody id="a_browsed"></tbody>
</table>

<script type="text/javascript">
  var __cat = '<?php echo $cat; ?>';
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __browsed;
  
  $(function(){
    __browsed = Cookie.get_cookie('a_browsed');
    if (!__browsed) {
      __browsed = [];
    } else {
      __browsed = JSON.parse(__browsed);
    }
    
    render_browsed();
  });
  
  function render_browsed() {
    $('#a_browsed').empty();
    for (var idx in __browsed) {
      var itm = __browsed[idx];
      $('#a_browsed').append(
        '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' +
          '<td>' +
            '<span class="date">' + itm.date.split(' ')[0] + '</span>' +
            '<a href="#" onclick="return Url.redirect(__redmd, \'' + __sys + '.php?i=' + __id + '&__file=index_article&id=' + itm.id + '&cat=' + itm.__cat + '\');">' + itm.title + '</a>' +
          '</td>' +
        '</tr>'
      );
    }
    for (var i = 10 - __browsed.length, il = 0; i > il; i--) {
      var itm = __browsed[idx];
      $('#a_browsed').append('<tr class="' + ((i % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '"><td>&nbsp;</td></tr>');
    }
    $('#a_browsed').append('<tr class="pure-table-even"><td style="text-align: right;"><a href="#" onclick="Cookie.set_cookie(\'a_browsed\', \'[]\', -1); __browsed = []; render_browsed(); return false;">清除</a></td></tr>');
  };
</script>
